 <?php
	// Session
	session_start();
	
	// Integrimi i skedave sistemore
    include('../include/functions.inc.php');
	include('../include/template.class.php');
	
	
	// DB CONNECT
	dbOpen("comunity");
	
	
	// PROJECT
	$PROJECT = setProject();
	
	
	// Ndyshoret primare
	define('TPL','../include/styles/'.$PROJECT['style'].'/comunity/index.html');
	define('FILENAME',basename($_SERVER["PHP_SELF"]));
	define('SID', setSid($_GET['id']));
	define('UID', $_SESSION['user_id']);

	
	// TMP CLASS
	$tmp = new Template(TPL,$PROJECT['lang']);
	
	
	// SPLASH SCREEN
	if (isset($_GET['splash'])) 
		splash($_GET['splash'], "../comunity/index.php?id=$_GET[id]");
	
	
	
	// INDEXING ARRAYS
	$ACCESS = $tmp->indexing("access");
	$SECTION = indexing("SELECT id,name FROM sections");

	
	// MENUES
	$tmp->setContent("CATEGORY",$tmp->menu("category","../comunity"));
	$tmp->setContent("SECTIONS", sections(SID));
	$tmp->setContent("FOOTMENU",$tmp->menu("foot"));

	
	
	// TOPBOOKS
	$query = mysql_query("SELECT id,author_id,volume,title
						  FROM library 
						  WHERE sid='".SID."' AND access != '0' and approve = '0'
						  ORDER BY counter DESC LIMIT 5");
        
	// Nese gjen ndonje resht ne arkive
	if (mysql_num_rows($query) > 0) {
		
		while ($BOOK = mysql_fetch_assoc($query)) {
			
			// Cakto emrin e autorit nga tabela e autoreve
			$BOOK['author'] = implode(mysql_fetch_assoc(mysql_query("SELECT name FROM authors WHERE id='".$BOOK['author_id']."'")));
			
			$TOPBOOKS[] = $BOOK;	  
		}
	}
	else 
		$TOPBOOKS = array();
	
	// ACCORDIONS	
	$tmp->setContent("TOPBOOKS",$TOPBOOKS);
	$tmp->setContent("BOOKMARK",bookmarks(UID,SID));
	$tmp->setContent("NEWS",news("comunity"));
	$tmp->setContent("RANDOM",randomContent("comunity"));
	
	
	// PAGE (current,offset,totalRows,maxPage,links,index)
	$PAGE['query'] = "SELECT id FROM library WHERE sid = '".SID."'";
	$PAGE = setPage(SID,$_GET['page'],FILENAME,$PAGE['query']);
	$PAGE['section'] = $SECTION[SID];
	$tmp->setContent($PAGE);
			
	
	// BOOKS QUERY
	$query = mysql_query("SELECT id,typ,author_id,volume,title,pubdate,counter,access 
						  FROM library 
						  WHERE sid = ".SID." AND access != '0' AND approve = '0'
						  ORDER BY pubdate DESC 
						  LIMIT ".$PAGE['offset'].",".$PROJECT['booksPerPage']."");
							
	
	if (mysql_num_rows($query) > 0) {
		
		while ($ROW = mysql_fetch_assoc($query)) {
			
			// Author
			$ROW['author'] = implode(mysql_fetch_assoc(mysql_query("SELECT name FROM authors WHERE id=".$ROW['author_id']."")));
			
			// Comments
			$ROW['comments'] = mysql_num_rows(mysql_query("SELECT id FROM comments WHERE book_id='".$ROW['id']."'"));
			
			// Bookmarks
			$ROW['bookmarks'] = mysql_num_rows( mysql_query("SELECT id FROM bookmarks WHERE book_id='".$ROW['id']."'"));
			
			// Rating
			$ROW['value'] = ratingValue($ROW['id']);
			
			// Publication date
			$ROW['pubdate'] = setDate($ROW['pubdate']);
			
			
			// ICONS
			$IA = array();
			$IA = mysql_fetch_assoc(mysql_query("SELECT bookmark,completed FROM bookmarks WHERE book_id = '".$ROW['id']."' AND  user_id = '".UID."' "));
			$ROW['icoType'] = $ROW['typ'];
			$ROW['icoAccess'] = ($ROW['access'] < 2) ? "0" : "1";
			$ROW['icoCompleted'] = (empty($IA['completed'])) ? "0" : $IA['completed'];
			$ROW['icoBookmark'] = (empty($IA['bookmark'])) ? "0" : $IA['bookmark'];
			
			// ALL BOOKS
			$BOOKS[] = $ROW;
		}
		
		$MESSAGE = array();
	}
	else {
		
		$BOOKS = array();
		
		$MESSAGE = $tmp->xmlContent("nobooks");
	}
	
	
	// BOOKS
	$tmp->setContent("MESSAGE",$MESSAGE);
	
	
	// BOOKS
	$tmp->setContent("BOOKS",$BOOKS);
	
	// LABELS
	$tmp->setLabels();

	// DISPLAY ALL						  
	echo $tmp->vorlage;
?>